package com.zxbit.uangstore.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cb.ratingbar.CBRatingBar;
import com.facebook.drawee.view.SimpleDraweeView;
import com.zxbit.uangstore.R;
import com.zxbit.uangstore.base.BaseRecyclerAdapter;
import com.zxbit.uangstore.domain.ProductItemData;
import com.zxbit.uangstore.utils.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

@SuppressLint("CheckResult") // 历史记录适配器
public class HistoryAdapter extends BaseRecyclerAdapter<ProductItemData, HistoryAdapter.HistoryViewHolder> {

    OnHistoryListener listener;

    public HistoryAdapter(Context context, OnHistoryListener listener) {
        super(context);
        this.listener = listener;
    }

    @NonNull
    @Override
    public HistoryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        return new HistoryViewHolder(inflater.inflate(R.layout.item_of_history_layout, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull HistoryViewHolder productViewHolder, int i) {
        final ProductItemData productItemData = mDatas.get(i);

        productViewHolder.tv_product_name.setText(productItemData.getProductName());
        if ("1".equals(productItemData.getIsboutique())) {
            productViewHolder.tv_boutique.setVisibility(View.VISIBLE);
        } else {
            productViewHolder.tv_boutique.setVisibility(View.GONE);
        }
        if (!StringUtils.isEmpty(productItemData.getLogoUrl())) {
            productViewHolder.img_logo.setImageURI(productItemData.getLogoUrl());
        }
        if (!StringUtils.isEmpty(productItemData.getScore())) {
            productViewHolder.tv_score.setText(productItemData.getScore());
            productViewHolder.star_score.setStarProgress(Float.parseFloat(productItemData.getScore()));
        }
        productViewHolder.img_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onHistoryClick(view, productItemData.getId());
                }
            }
        });

    }

    class HistoryViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_logo)
        SimpleDraweeView img_logo; // LOGO
        @BindView(R.id.tv_product_name)
        TextView tv_product_name; // APP名称
        @BindView(R.id.tv_boutique)
        TextView tv_boutique; // 精选
        @BindView(R.id.tv_score)
        TextView tv_score; // 评分
        @BindView(R.id.star_score)
        CBRatingBar star_score; // 星级
        @BindView(R.id.img_delete)
        ImageView img_delete; // 删除按钮

        HistoryViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnHistoryListener {
        void onHistoryClick(View view, String productId);
    }

}
